/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


var Importer = {
    pathConfig: function (type) {
        if(type === "js"){
            return Importer.getSiteRoot(true) + "js/config/jspath.json";
        }
        return Importer.getSiteRoot(true) + "js/config/cspath.json";
    },

    init: function(jsPathArray, csPathArray){
        if(jsPathArray === undefined){
            window.alert("没有定义需要加载的依赖文件！");
            return;
        }

        var path = Importer.synAccept(Importer.pathConfig("js"), "GET");
        for (var i in jsPathArray) {
            var fun = new Function("path", "for (var j in path." + jsPathArray[i] + ") {\n\
                    if (Importer.checkURL(path." + jsPathArray[i] + "[j])) {\n\
                        Importer.JsLoader.load(path." + jsPathArray[i] + "[j]);\n\
                    } else {\n\
                        Importer.JsLoader.load(Importer.getSiteRoot(true) + path." + jsPathArray[i] + "[j]);\n\
                    }\n\
                }");
            fun(path);
        }

        if (csPathArray !== undefined) {
            path = Importer.synAccept(Importer.pathConfig("css"), "GET");
            for (var i in csPathArray) {
                var fun = new Function("path", "for (var j in path." + csPathArray[i] + ") {\n\
                    if (Importer.checkURL(path." + csPathArray[i] + "[j])) {\n\
                        Importer.CsLoader.load(path." + csPathArray[i] + "[j]);\n\
                    } else {\n\
                        Importer.CsLoader.load(Importer.getSiteRoot(true) + path." + csPathArray[i] + "[j]);\n\
                    }\n\
                }");
                fun(path);
            }
        }
    },

    getSiteRoot: function (isVirtual) {
        var siteRoot = window.location.protocol + "//" + window.location.host + "/";
        if (!isVirtual)
            return siteRoot;

        var relativePath = window.location.pathname;
        if (relativePath != "" && relativePath.substring(0, 1) == "/") {
            //此处重要，不同的浏览器可能返回的relativePath不一样
            relativePath = relativePath.substring(1);
        }
        var virtualPath = (relativePath == "") ? "" : relativePath.substring(0, relativePath.indexOf("/") + 1);

        return siteRoot + virtualPath;
    },

    synAccept: function(url, type){
        var data;
        var xmlHttp;
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
            if (xmlHttp.overrideMimeType)
                xmlHttp.overrideMimeType('text/xml');
        } else if (window.ActiveXObject) {
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    window.alert("该浏览器不支持加载！");
                }
            }
        }
        xmlHttp.open(type, url, false);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xmlHttp.send();
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            data = eval('(' + xmlHttp.responseText + ')');
        } else {
            window.alert("加载出错！");
        }
        return data;
    },

    checkURL: function(URL) {
        var str = URL;
        var Expression = /http(s)?:\/\/*?/;
        var objExp = new RegExp(Expression);
        if (objExp.test(str) == true) {
            return true;
        } else {
            return false;
        }
    }
};

Importer.JsLoader = {
    load: function (sUrl) {
        var _script = document.createElement('script');
        _script.setAttribute('charset', 'UTF-8');
        _script.setAttribute('type', 'text/javascript');
        _script.setAttribute('src', sUrl);
        document.getElementsByTagName('head')[0].appendChild(_script);
    }
};

Importer.CsLoader = {
    load: function (sUrl) {
        var _link = document.createElement('link');
        _link.setAttribute('type', 'text/css');
        _link.setAttribute('rel', 'stylesheet');
        _link.setAttribute('href', sUrl);
        document.getElementsByTagName('head')[0].appendChild(_link);
    }
};